#企业微信地址
import os
import pytest
from selenium import webdriver

#简单工厂模式调用
from bo.page_factory import PageFactory

def page_fa_init():
    page_factory = PageFactory()
    return page_factory

#chrome浏览器，有界面模式
@pytest.fixture(scope="session")
def chrome_dirver():
    print("开始执行")
    driver = webdriver.Chrome()
    driver.implicitly_wait(5) #隐式等待
    index = page_fa_init().create_page_object('index', driver)
    yield index
    print("执行结束")
    driver.close()
    driver.quit()

#firefox浏览器，有界面模式
def firefox_driver():
    print("开始执行")
    driver = webdriver.Firefox()
    driver.implicitly_wait(5)
    index = page_fa_init().create_page_object('index', driver)
    yield index
    print('执行结束')
    driver.close()
    driver.quit()

#Chrome浏览器复用模式
@pytest.fixture(scope="session")
def chrome_debug_driver():
    print("开始执行")
    option = webdriver.ChromeOptions()
    option.add_experimental_option('w3c',False)
    option.debugger_address = '127.0.0.1:9222'
    driver = webdriver.Chrome(options=option)
    driver.implicitly_wait(5) #隐式等待
    index = page_fa_init().create_page_object('index', driver)
    yield index
    print("执行结束")
    driver.close()
    driver.quit()

#chrome无界面模式
@pytest.fixture(scope="session")
def chrome_headless_dirver():
    print("开始执行")
    option = webdriver.ChromeOptions()
    option.add_argument('--headless')  # 无界面运行
    driver = webdriver.Chrome(options=option)
    driver.implicitly_wait(5) #隐式等待
    index = page_fa_init().create_page_object('index', driver)
    yield index
    print("执行结束")
    driver.close()
    driver.quit()

@pytest.fixture(scope="session")
def firefox_headlss_driver():
    print("开始执行")
    option = webdriver.FirefoxOptions()
    option.add_argument('--headless')
    driver = webdriver.Firefox(options=option)
    driver.implicitly_wait(5)
    index = page_fa_init().create_page_object('index', driver)
    yield index
    print('执行结束')
    driver.close()
    driver.quit()

#多浏览
@pytest.fixture(scope="session")
def get_driver():
    browser = os.getenv('browser')

    if browser == 'firefox':
        print('启动火狐浏览器')
        option_firefox = webdriver.FirefoxOptions()
        option_firefox.add_argument('--headless')
        driver = webdriver.Firefox(options=option_firefox)

    if browser == 'chrome':
        print('启动谷歌浏览器')
        option_chrome = webdriver.ChromeOptions()
        option_chrome.add_argument('--headless')
        driver = webdriver.Chrome(options=option_chrome)

    driver.implicitly_wait(5)

    index = page_fa_init().create_page_object('index', driver)
    yield index

    print('关闭浏览器')
    driver.close()
    driver.quit()